data {
  int<lower=0> n;        // number of obs
  int<lower=0> m;        // number of studies
  int<lower=0> nX;       // number of individual-level parameters
  int<lower=0> nZ;	      // number of study-level parameters
  matrix[n,nX] X;        // predictor matrix indiviual level
  matrix[m,nZ] Z;        // predictor matrix study level
  int<lower=0,upper=1> svo[n];	 // dependent variable
  int<lower=0> study[n]; // study indicator
}
parameters {
  vector[m] beta0;
  vector[nX] beta;
  vector[nZ] gamma;
  real<lower=0> sigma_beta0;
  real<lower=0> sigma_beta;
} 
model {
  vector[n] svo_hat;

  for (i in 1:n)
    svo_hat[i] <- beta0[study[i]] + X[i] * beta;
  beta0 ~ normal(Z * gamma, sigma_beta0);
  svo ~ bernoulli_logit(svo_hat);
  
  beta ~ normal(0,5);
  gamma ~ normal(0,5);
  //sigma_beta0 ~ cauchy(0,5);
}
